package com.sharer.last.controller;

import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.interfaces.ValidateGroup;
import com.sharer.last.model.dto.comment.CommentChangeDTO;
import com.sharer.last.model.dto.comment.CommentQueryDTO;
import com.sharer.last.model.dto.comment.CommentUpdateDTO;
import com.sharer.last.model.vo.comment.CommentVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sharer.last.annotation.OperationLogger;
import com.sharer.last.service.CommentService;

import javax.annotation.Resource;

/**
 * 博客评论后台管理
 *
 * @ClassName CommentAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:03
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/comment")
public class CommentAdminController {
    @Resource
    CommentService commentService;

    /**
     * 根据id查询数据
     *
     * @param id 评论id
     * @return
     */
    @PostMapping("/findById/{id}")
    public R<CommentVO> findById(@PathVariable("id") Long id) {
        CommentVO commentVO = commentService.findById(id);
        return R.success(commentVO);
    }

    /**
     * 按页面和博客id分页查询评论List
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<CommentVO>> page(@RequestBody CommentQueryDTO queryDTO) {
        PageVO<CommentVO> list = commentService.selectListByPage(queryDTO);
        return R.success(list);
    }

    /**
     * 新增
     *
     * @param dto 评论实体
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody @Validated(ValidateGroup.InsertGroup.class) CommentChangeDTO dto) {
        commentService.createComment(dto);
        return R.handle();
    }

    /**
     * 修改评论
     *
     * @param dto 评论实体
     * @return
     */
    @OperationLogger("修改评论")
    @PutMapping("/update")
    public R update(@RequestBody @Validated(ValidateGroup.UpdateGroup.class) CommentChangeDTO dto) {
        commentService.updateComment(dto);
        return R.handle();
    }

    /**
     * 更新评论公开状态
     *
     * @return
     */
    @OperationLogger("更新评论公开状态")
    @PutMapping("/updatePublished")
    public R updatePublished(@RequestBody CommentUpdateDTO dto) {
        commentService.updateCommentPublishedById(dto.getId(), dto.getIsPublished());
        return R.handle();
    }

    /**
     * 更新评论接收邮件提醒状态
     *
     * @return
     */
    @OperationLogger("更新评论邮件提醒状态")
    @PutMapping("/updateNotice")
    public R updateNotice(@RequestBody CommentUpdateDTO dto) {
        commentService.updateCommentNoticeById(dto.getId(), dto.getIsNotice());
        return R.handle();
    }

    /**
     * 按id删除该评论及其所有子评论
     *
     * @param id 评论id
     * @return
     */
    @OperationLogger("删除评论")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        commentService.removeCommentById(id);
        return R.handle();
    }
}
